<?php
if (isset($imageAttachments) && count($imageAttachments)) {
?>
<h3><?php __('Attachments')?></h3>
    <table cellpadding="0" cellspacing="0">
    <?php
        $tableHeaders = $this->Html->tableHeaders(array(
            '&nbsp;',
            $this->Paginator->sort('title'),
            __('Name', true),	    
	    $this->Paginator->sort('mime_type'),
	    $this->Paginator->sort('size'),	    
            __('Actions', true),
        ));
        echo $tableHeaders;

        $rows = array();
        foreach ($imageAttachments AS $attachment) {
	    $actions = '';
            /*$actions  = $this->Html->link(__('Edit', true), array(
                'controller' => 'attachments',
                'action' => 'edit', 
                $attachment['Attachment']['id'],
            ));*/
            $actions .= ' ' . $this->Layout->adminRowActions($attachment['Attachment']['id']);
            $actions .= ' ' . $this->Html->link(__('Delete', true), array(
                'controller' => 'attachments',
                'action' => 'delete',
                $attachment['Attachment']['id'],
                'token' => $this->params['_Token']['key'],
            ), null, __('Are you sure?', true));            
	    
	    $thumbnail = '';
	    try {
		$thumbnail = $this->Html->link($this->Image->resize('uploads' . DS . 'images' . DS .$attachment['Attachment']['filename'], 120, 120, array(), true), '#', array(                    
			'escape' => false,
		    ));	    	    
	    } catch (Exception $e) {}
	    
	    if (isset($this->params['named']['editor'])) {
		$thumbnail .= '<br />' . $this->Html->link(__('Insert thumbnail', true), '#', array(
			'onclick' => "selectURL('thumb','" . $attachment['Attachment']['filename'] . "');",
			'escape' => false,
		    ));
		$thumbnail .= '<br />' . $this->Html->link(__('Insert full size', true), '#', array(
			'onclick' => "selectURL('image','" . $attachment['Attachment']['filename'] . "');",
			'escape' => false,
		    ));		
	    }	    

            $rows[] = array(
                $thumbnail,		
                $attachment['Attachment']['title'],
		$this->Html->link($attachment['Attachment']['filename'], '/uploads/images/' . $attachment['Attachment']['filename']),
		round($attachment['Attachment']['size'] / 1024, 1) . ' Kib',
		$attachment['Attachment']['mime_type'],
                $actions,
            );
        }

        echo $this->Html->tableCells($rows);
        echo $tableHeaders;
    ?>
    </table>

<div class="paging"><?php echo $paginator->numbers(); ?></div>
<div class="counter"><?php echo $paginator->counter(array('format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true))); ?></div>
<?php
} else {
    __('No attachments found');
}
?>